import { _decorator, AudioClip, Component, Node, Slider } from 'cc'
const { ccclass, property } = _decorator
import { AudioMgr } from './Utils/AudioMgr'
import { GAME_STATUS } from './Events'
import { GAME_STATUS_ENUM } from './Enums'
import { DataManager } from './DataManager'

@ccclass('GameManager')
export class GameManager extends Component {
  @property(AudioClip)
  bgm: AudioClip = null!
  // 成功
  @property(AudioClip)
  completeSound: AudioClip = null!
  // 失败
  @property(AudioClip)
  losedSound: AudioClip = null!

  @property(Node)
  gameCompleteNode: Node = null!
  @property(Node)
  gameLosedNode: Node = null!

  protected onLoad(): void {
    GAME_STATUS.on(GAME_STATUS_ENUM.COMPLETE, this.gameComplete, this)
    GAME_STATUS.on(GAME_STATUS_ENUM.LOSED, this.gameLosed, this)
  }
  start() {
    // AudioMgr.inst.play(this.bgm, 0.5, true)
  }
  gameComplete() {
    DataManager.inst.gameStatus = GAME_STATUS_ENUM.COMPLETE
    this.gameCompleteNode.active = true
    AudioMgr.inst.playOneShot(this.completeSound)
    console.log('游戏完成')
  }
  gameLosed() {
    this.gameLosedNode.active = true
    AudioMgr.inst.playOneShot(this.losedSound)
    console.log('游戏结束')
  }
  reStart() {
    this.gameCompleteNode.active = false
    this.gameLosedNode.active = false
    DataManager.inst.reStart()
    GAME_STATUS.emit(GAME_STATUS_ENUM.RUNNING)
  }
  nextLevel() {
    this.gameCompleteNode.active = false
    DataManager.inst.reStart()
    GAME_STATUS.emit(GAME_STATUS_ENUM.RUNNING)
    console.log('下一关')
  }
}
